package com.copa.l389;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author copa
 * @createDate 2022-07-06 10:49
 * @function 哈希表法
 */
public class Test2 {

    public char findTheDifference(String s, String t) {
        // 所谓哈希，其实可以用数组代替，应用好ascii码
        int[] table = new int[26];
        if (s.length() == 0) {
            return t.charAt(0);
        }

        char[] tChars = t.toCharArray();
        char[] sChars = s.toCharArray();
        // 妙就妙在这里，一个数字循环利用 s字符串加到对应的数组，t字符串加到对应的数组，若该数组某一个元素<0，则是多出来的字母
        for (int i = 0; i < tChars.length; i++) {
            if (i < sChars.length) {
                table[sChars[i] - 'a']++;
            }
            table[tChars[i] - 'a']--;
        }

        for (int i = 0; i < table.length; i++) {
            if (table[i] < 0) {
                return (char) ('a' + i);
            }
        }
        return '0';
    }

    // 一开始能想到的...嫌麻烦可以忽略...
//    public char findTheDifference(String s, String t) {
//        HashMap<Character, Integer> sMap = new HashMap<>();
//        HashMap<Character, Integer> tMap = new HashMap<>();
//        char[] sChars = s.toCharArray();
//        char[] tChars = t.toCharArray();
//        for (char sChar : sChars) {
//            if (!sMap.containsKey(sChar)) {
//                sMap.put(sChar, 0);
//            }
//            sMap.put(sChar, sMap.get(sChar) + 1);
//        }
//        for (char tChar : tChars) {
//            if (!tMap.containsKey(tChar)) {
//                tMap.put(tChar, 0);
//            }
//            tMap.put(tChar, tMap.get(tChar) + 1);
//        }
//        for (Map.Entry<Character, Integer> characterIntegerEntry : tMap.entrySet()) {
//            if (!sMap.containsKey(characterIntegerEntry.getKey())
//                    || !Objects.equals(characterIntegerEntry.getValue(), sMap.get(characterIntegerEntry.getKey()))) {
//                return characterIntegerEntry.getKey();
//            }
//        }
//        return '0';
//    }

    public static void main(String[] args) {
//        String s = "abcd";
//        String t = "abcde";
        String s = "ymbgaraibkfmvocpizdydugvalagaivdbfsfbepeyccqfepzvtpyxtbadkhmwmoswrcxnargtlswqemafandgkmydtimuzvjwxvlfwlhvkrgcsithaqlcvrihrwqkpjdhgfgreqoxzfvhjzojhghfwbvpfzectwwhexthbsndovxejsntmjihchaotbgcysfdaojkjldprwyrnischrgmtvjcorypvopfmegizfkvudubnejzfqffvgdoxohuinkyygbdzmshvyqyhsozwvlhevfepdvafgkqpkmcsikfyxczcovrmwqxxbnhfzcjjcpgzjjfateajnnvlbwhyppdleahgaypxidkpwmfqwqyofwdqgxhjaxvyrzupfwesmxbjszolgwqvfiozofncbohduqgiswuiyddmwlwubetyaummenkdfptjczxemryuotrrymrfdxtrebpbjtpnuhsbnovhectpjhfhahbqrfbyxggobsweefcwxpqsspyssrmdhuelkkvyjxswjwofngpwfxvknkjviiavorwyfzlnktmfwxkvwkrwdcxjfzikdyswsuxegmhtnxjraqrdchaauazfhtklxsksbhwgjphgbasfnlwqwukprgvihntsyymdrfovaszjywuqygpvjtvlsvvqbvzsmgweiayhlubnbsitvfxawhfmfiatxvqrcwjshvovxknnxnyyfexqycrlyksderlqarqhkxyaqwlwoqcribumrqjtelhwdvaiysgjlvksrfvjlcaiwrirtkkxbwgicyhvakxgdjwnwmubkiazdjkfmotglclqndqjxethoutvjchjbkoasnnfbgrnycucfpeovruguzumgmgddqwjgdvaujhyqsqtoexmnfuluaqbxoofvotvfoiexbnprrxptchmlctzgqtkivsilwgwgvpidpvasurraqfkcmxhdapjrlrnkbklwkrvoaziznlpor";
        String t = "qhxepbshlrhoecdaodgpousbzfcqjxulatciapuftffahhlmxbufgjuxstfjvljybfxnenlacmjqoymvamphpxnolwijwcecgwbcjhgdybfffwoygikvoecdggplfohemfypxfsvdrseyhmvkoovxhdvoavsqqbrsqrkqhbtmgwaurgisloqjixfwfvwtszcxwktkwesaxsmhsvlitegrlzkvfqoiiwxbzskzoewbkxtphapavbyvhzvgrrfriddnsrftfowhdanvhjvurhljmpxvpddxmzfgwwpkjrfgqptrmumoemhfpojnxzwlrxkcafvbhlwrapubhveattfifsmiounhqusvhywnxhwrgamgnesxmzliyzisqrwvkiyderyotxhwspqrrkeczjysfujvovsfcfouykcqyjoobfdgnlswfzjmyucaxuaslzwfnetekymrwbvponiaojdqnbmboldvvitamntwnyaeppjaohwkrisrlrgwcjqqgxeqerjrbapfzurcwxhcwzugcgnirkkrxdthtbmdqgvqxilllrsbwjhwqszrjtzyetwubdrlyakzxcveufvhqugyawvkivwonvmrgnchkzdysngqdibhkyboyftxcvvjoggecjsajbuqkjjxfvynrjsnvtfvgpgveycxidhhfauvjovmnbqgoxsafknluyimkczykwdgvqwlvvgdmufxdypwnajkncoynqticfetcdafvtqszuwfmrdggifokwmkgzuxnhncmnsstffqpqbplypapctctfhqpihavligbrutxmmygiyaklqtakdidvnvrjfteazeqmbgklrgrorudayokxptswwkcircwuhcavhdparjfkjypkyxhbgwxbkvpvrtzjaetahmxevmkhdfyidhrdeejapfbafwmdqjqszwnwzgclitdhlnkaiyldwkwwzvhyorgbysyjbxsspnjdewjxbhpsvj";
        System.out.println(new Test2().findTheDifference(s, t));
    }
}
